import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter04/widget/my_dialog.dart';
import 'package:fluttertoast/fluttertoast.dart' as fluttertoast;
import 'package:ftoast/ftoast.dart' as ftoast;

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _alertDialog() async {
    if (kDebugMode) {
      print("_alertDialog");
    }
    var result = await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return AlertDialog(
          title: const Text("提示信息"),
          content: const Text("您确定要删除吗？"),
          actions: [
            TextButton(
              onPressed: () {
                if (kDebugMode) {
                  print("ok");
                }
                Navigator.of(context).pop("ok");
              },
              child: const Text("确定"),
            ),
            TextButton(
              onPressed: () {
                if (kDebugMode) {
                  print("cancel");
                }
                Navigator.of(context).pop("取消");
              },
              child: const Text("取消"),
            ),
          ],
        );
      },
    );
    if (kDebugMode) {
      print("result - $result");
    }
  }

  void _showModalBottomSheet() async {
    if (kDebugMode) {
      print("_showModalBottomSheet");
    }
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return SizedBox(
          height: 240,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ListTile(
                title: const Text("分享"),
                onTap: () {
                  if (kDebugMode) {
                    print("分享");
                  }
                  Navigator.pop(context, "分享");
                },
              ),
              const Divider(),
              ListTile(
                title: const Text("收藏"),
                onTap: () {
                  if (kDebugMode) {
                    print("收藏");
                  }
                  Navigator.pop(context, "收藏");
                },
              ),
              const Divider(),
              ListTile(
                title: const Text("取消"),
                onTap: () {
                  if (kDebugMode) {
                    print("取消");
                  }
                  Navigator.pop(context, "取消");
                },
              ),
            ],
          ),
        );
      },
    );
    if (kDebugMode) {
      print("result - $result");
    }
  }

  void _simpleDialog() async {
    if (kDebugMode) {
      print("_simpleDialog");
    }
    var result = await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return SimpleDialog(
          title: const Text("请选择预言"),
          children: [
            SimpleDialogOption(
              onPressed: () {
                if (kDebugMode) {
                  print("简体中文");
                }
                Navigator.of(context).pop("简体中文");
              },
              child: const Text("简体中文"),
            ),
            const Divider(),
            SimpleDialogOption(
              onPressed: () {
                if (kDebugMode) {
                  print("英文");
                }
                Navigator.pop(context, "英文");
              },
              child: const Text("英文"),
            ),
            const Divider(),
            SimpleDialogOption(
              onPressed: () {
                if (kDebugMode) {
                  print("日语");
                }
                Navigator.pop(context, "日语");
              },
              child: const Text("日语"),
            ),
          ],
        );
      },
    );
    if (kDebugMode) {
      print("result - $result");
    }
  }

  void _alertToast() {
    if (kDebugMode) {
      print("_alertToast");
    }
    fluttertoast.Fluttertoast.showToast(
      msg: "提示信息。。。",
      toastLength: fluttertoast.Toast.LENGTH_SHORT,
      gravity: fluttertoast.ToastGravity.TOP,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0,
    );
  }

  void _alertFtoast() {
    if (kDebugMode) {
      print("_alertFtoast");
    }
    ftoast.FToast.toast(context, msg: "FToast");
  }

  void _myDialog() async {
    if (kDebugMode) {
      print("_myDialog");
    }
    var result = await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return MyDialog(
          title: "警告",
          content: "我是警告信息啊",
          onClose: () {
            if (kDebugMode) {
              print("close");
            }
            Navigator.pop(context, "close");
          },
        );
      },
    );
    if (kDebugMode) {
      print("result -- $result");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Dialog演示"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _alertDialog,
              child: const Text("alert弹出框-AlertDialog"),
            ),
            const SizedBox(
              height: 10,
            ),
            ElevatedButton(
              onPressed: _simpleDialog,
              child: const Text("select弹出框-SimpleDialog"),
            ),
            const SizedBox(
              height: 10,
            ),
            ElevatedButton(
              onPressed: _showModalBottomSheet,
              child: const Text("ActionSheet弹出框-showModalBottomSheet"),
            ),
            const SizedBox(
              height: 10,
            ),
            ElevatedButton(
              onPressed: _alertToast,
              child: const Text("Toast-fluttertoast"),
            ),
            const SizedBox(
              height: 10,
            ),
            ElevatedButton(
              onPressed: _alertFtoast,
              child: const Text("Toast-ftoast"),
            ),
            const SizedBox(
              height: 10,
            ),
            ElevatedButton(
              onPressed: _myDialog,
              child: const Text("自定义Dialog"),
            ),
            const SizedBox(
              height: 10,
            ),
          ],
        ),
      ),
    );
  }
}
